package com.fishbone.mybatisplus;

import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import com.fishbone.core.constants.AppConstant;
import com.fishbone.mybatisplus.generator.MybatisPlusGeneratorProperties;
import com.fishbone.mybatisplus.injector.ExtendDefaultSqlInjector;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName MybatisPlusConfiguration
 * @Author huyaxi
 * @DATE 2020/1/19 13:24
 */
@Configuration
@MapperScan(AppConstant.BASE_MAPPER_PACKAGES)
@EnableConfigurationProperties({MybatisPlusGeneratorProperties.class})
public class MybatisPlusAutoConfiguration {


    /**
     * 分页拦截器
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }


    /**
     * 扩展SQL注入
     * @return
     */
    @Bean
    public ISqlInjector sqlInjector() {
        return new ExtendDefaultSqlInjector();
    }

}
